package com.shzu.jwc.ty.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.Msg;
import com.shzu.jwc.ty.bean.ProblemClass;
import com.shzu.jwc.ty.bean.SelectProblem;
import com.shzu.jwc.ty.service.ProblemClassService;
import com.shzu.jwc.ty.service.SelectProblemservice;

@Controller
@RequestMapping("selectproblem")
public class SelectProblemcontroller {
	@Autowired
	SelectProblemservice selectproblemservice;
	
	@Autowired
	ProblemClassService problemclassservice;
	
	@RequestMapping("/selectproblem.action")
	public String showselectproblem(@RequestParam(value="pn", defaultValue="1") Integer pn,Model model)
	{
		PageHelper.startPage(pn,10);
		List<SelectProblem> selectdata=selectproblemservice.getselectproblem(0);
		List<ProblemClass> classdata=problemclassservice.getproblemclass();
		PageInfo pageinfo=new PageInfo(selectdata,10);
		model.addAttribute("classdata", classdata);
		model.addAttribute("pageinfo", pageinfo);
		return "/selectproblem/selectproblem";
	}
	
	@ResponseBody
	@RequestMapping("/uploadselect.action")
	public Msg uploadselect(@RequestParam("file") MultipartFile file) throws Exception
	{
		if(file.isEmpty())
			return Msg.error("题库不能为空");
		List<String> list=new ArrayList<String>();
		Integer error=0;
		String errormessge="";
		Integer statue=null;
		SelectProblem selectProblem=new SelectProblem();
		String[] a=new String[10];
		BufferedReader br=new BufferedReader(new InputStreamReader(file.getInputStream(),"GBK"));
		String stemp;
		br.readLine();
		while((stemp = br.readLine()) != null)
		{
			list.add(stemp);
		}
		for(Integer i=0;i<list.size();i++) {
			a=list.get(i).split(",");
			selectProblem.setSelectProblemTitle(a[0]);
			selectProblem.setSelectProblemOption1(a[1]);
			selectProblem.setSelectProblemOption2(a[2]);
			selectProblem.setSelectProblemOption3(a[3]);
			selectProblem.setSelectProblemOption4(a[4]);
			selectProblem.setSelectProblemRightoption(a[5]);
			statue=problemclassservice.valuegetid(a[6]);
			if(statue==404) {
				error++;
				errormessge=errormessge+(i+2)+" ";
				continue;
			}
			selectProblem.setProblemClassId(problemclassservice.valuegetid(a[6]));
			selectProblem.setSelectProblemStatue(0);
			selectproblemservice.insertselect(selectProblem);
		}
		if(error==0)
			return Msg.success("导入成功");
		else
			return Msg.success("导入成功，共"+error+"条错误，错误为第"+errormessge+"行");
	}
	
	@ResponseBody
	@RequestMapping(value="/insertselect.action")
	public Msg insertselect(SelectProblem selectProblem)
	{
		selectProblem.setSelectProblemStatue(0);
		if(selectproblemservice.insertselect(selectProblem)>0)
			return Msg.success("插入成功");
		else
			return Msg.error("插入失败");
	}
	
	@ResponseBody
	@RequestMapping("/deleteselect.action")
	public Msg deleteselect(@RequestParam("id") Integer selectProblemId)
	{
		if(selectproblemservice.deleteselect(selectProblemId)>0)
			return Msg.success("删除成功");
		else
			return Msg.error("删除失败");
	}
	
	@ResponseBody
	@RequestMapping("/updateselectproblem.action")
	public Msg updateselectproblem(@RequestParam("id") Integer id,@RequestParam("column") String column,@RequestParam("value") String value)
	{
		if(selectproblemservice.updateselect(id, column, value)>0)
			return Msg.success("修改成功");
		else
			return Msg.error("修改失败");
	}
	
	@RequestMapping("/searchselect.action")
	public String searchselect(@RequestParam("problemClassId") Integer problemClassId,@RequestParam(value="pn", defaultValue="1") Integer pn,Model model,HttpSession session)
	{
		List<ProblemClass> classdata=problemclassservice.getproblemclass();
		if(problemClassId==0)
		{
			PageHelper.startPage(pn,10);
			List<SelectProblem> selectdata=selectproblemservice.getselectproblem(0);
			PageInfo pageinfo=new PageInfo(selectdata,10);	
			model.addAttribute("pageinfo", pageinfo);
		}
		else
		{
			PageHelper.startPage(pn,10);
			List<SelectProblem> selectdata=selectproblemservice.getsearchselectproblem(0, problemClassId);
			PageInfo pageinfo=new PageInfo(selectdata,10);	
			model.addAttribute("pageinfo", pageinfo);
			session.setAttribute("problemClassId", problemClassId);
		}
		model.addAttribute("classdata", classdata);
		return "/selectproblem/selectproblem";
	}
}
